package com.mrd.shiptask_shipowner.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;

import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.constants.database.DBUtil;
import com.mrd.shiptask_shipowner.serviecs.initdatadao.InitDatasDaoImpl;
import com.mrd.shiptask_shipowner.unit_tools.DatasTools;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.SysApplication;
import com.mrd.shiptask_shipowner.unit_tools.flesh_listview.MyListView;
import com.mrd.shiptask_shipowner.unit_tools.flesh_listview.MyListView.OnRefreshListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MakeToOrderListActivity extends Activity {

	private final MakeToOrderListActivity THIS = MakeToOrderListActivity.this;
	private List<Map<String, Object>> mData;
	private MyAdapter adapter;
	private Button backToBeforeBt;
	ProgressDialog progressDialog;

	@SuppressLint("SimpleDateFormat")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.make_to_order_list);
		setInitDatas();
		SysApplication.getInstance().addActivity(this);

	}

	public void setInitDatas() {
		progressDialog = new ProgressDialog(THIS);
		initListView();
		initViewAction();
	}

	private void initListView() {
		try {
			JSONArray jas = DBUtil.selectMakeToShipOrderTable(THIS);
			List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < jas.length(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				String getId = DatasTools.getJsonValue(jas.getJSONObject(i),
						"ID");
				String getLoadTime = DatasTools.getJsonValue(
						jas.getJSONObject(i), "LOAD_TIME");
				String getLoadRequire = DatasTools.getJsonValue(
						jas.getJSONObject(i), "LOAD_REQUIRE");
				String getCode = DatasTools.getJsonValue(jas.getJSONObject(i),
						"CODE");
				String getCargoName = DatasTools.getJsonValue(
						jas.getJSONObject(i), "CARGO_NAME");
				String getStartPort = DatasTools.getJsonValue(
						jas.getJSONObject(i), "START_PORT");
				String getEndPort = DatasTools.getJsonValue(
						jas.getJSONObject(i), "END_PORT");
				String getRAmount = DatasTools.getJsonValue(
						jas.getJSONObject(i), "R_AMOUNT");
				String getCargoUnits = DatasTools.getJsonValue(
						jas.getJSONObject(i), "CARGO_UNITS");
				String getIncludePcf = DatasTools.getJsonValue(
						jas.getJSONObject(i), "INCLUDE_PCF");
				String getAccountMethod = DatasTools.getJsonValue(
						jas.getJSONObject(i), "ACCOUNT_METHOD");
				String getPackingType = DatasTools.getJsonValue(
						jas.getJSONObject(i), "PACKING_TYPE");
				String getConsignorCompany = DatasTools.getJsonValue(
						jas.getJSONObject(i), "CONSIGNOR_COMPANY");
				String getConsignor = DatasTools.getJsonValue(
						jas.getJSONObject(i), "CONSIGNOR");
				String getContact = DatasTools.getJsonValue(
						jas.getJSONObject(i), "CONTACT");
				String getExpectPrice = DatasTools.getJsonValue(
						jas.getJSONObject(i), "EXPECT_PRICE");
				String getFareTax = DatasTools.getJsonValue(
						jas.getJSONObject(i), "FARE_TAX");
				String getLossrate = DatasTools.getJsonValue(
						jas.getJSONObject(i), "LOSS_RATE");
				String getLoadUseTime = DatasTools.getJsonValue(
						jas.getJSONObject(i), "LOAD_USE_TIME");
				String getDemurrage = DatasTools.getJsonValue(
						jas.getJSONObject(i), "DEMURRAGE");
				String getShipTonnages = DatasTools.getJsonValue(
						jas.getJSONObject(i), "SHIP_TONNAGES");
				String getRemark = DatasTools.getJsonValue(
						jas.getJSONObject(i), "REMARK");
				map.put("getId", getId);
				map.put("getLoadTime", getLoadTime);
				map.put("getLoadRequire", getLoadRequire);
				map.put("getCode", getCode);
				map.put("getCargoName", getCargoName);
				map.put("getStartPort", getStartPort);
				map.put("getEndPort", getEndPort);
				map.put("getRAmount", getRAmount);
				map.put("getCargoUnits", getCargoUnits);
				map.put("getIncludePcf", getIncludePcf);
				map.put("getAccountMethod", getAccountMethod);
				map.put("getPackingType", getPackingType);
				map.put("getConsignorCompany", getConsignorCompany);
				map.put("getConsignor", getConsignor);
				map.put("getContact", getContact);
				map.put("getExpectPrice", getExpectPrice);
				map.put("getFareTax", getFareTax);
				map.put("getLossrate", getLossrate);
				map.put("getLoadUseTime", getLoadUseTime);
				map.put("getDemurrage", getDemurrage);
				map.put("getShipTonnages", getShipTonnages);
				map.put("getRemark", getRemark);
				mapList.add(map);
			}

			final MyListView listView = (MyListView) findViewById(R.id.listView1);
			// 生成动态数组，并且转入数据
			mData = mapList;
			// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
			adapter = new MyAdapter(this);
			listView.setAdapter(adapter);
			listView.setonRefreshListener(new OnRefreshListener() {

				@Override
				public void onRefresh() {
					new AsyncTask<Void, Void, Void>() {
						@Override
						protected Void doInBackground(Void... params) {
							try {
								Thread.sleep(2000);
								InitDatasDaoImpl.getShipMakeToOrderDatas(THIS);
							} catch (Exception e) {
								e.printStackTrace();
							}
							return null;
						}

						@Override
						protected void onPostExecute(Void result) {
							adapter.notifyDataSetChanged();
							listView.onRefreshComplete();
						}
					}.execute(null, null, null);
				}
			});
		} catch (Exception e) {
		}
	}

	private void initViewAction() {
		TextView topTitleTv = (TextView) findViewById(R.id.topTitleTv);
		topTitleTv.setText("货运申请列表");
		backToBeforeBt = (Button) findViewById(R.id.backToBeforeBt1);
		backToBeforeBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				FunctionOfActivity.progressDialogShow(THIS);
				startActivity(new Intent(THIS, MainActivity.class));
			}
		});
	}

	public final OnClickListener socl = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case 1:

				break;

			default:
				break;
			}
		}

	};

	public final class ViewHolder {
		private TextView tuoyunrenTv, huowuTv, hangxianTv, jiesuanTv,
				yunliangTv, jiezhiTv, qiwangTv;
		private Button takeOrderBt;
		private LinearLayout linear1;
	}

	public class MyAdapter extends BaseAdapter {

		private final LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@SuppressLint({ "NewApi", "ResourceAsColor" })
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			ViewHolder holder = null;

			holder = new ViewHolder();
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.make_to_order_listitem, null);
				// tuoyunrenTv, huowuTv, hangxianTv, jiesuanTv,
				// yunliangTv, jiezhiTv, qiwangTv
				holder.tuoyunrenTv = (TextView) convertView
						.findViewById(R.id.tuoyunrentv);
				holder.huowuTv = (TextView) convertView
						.findViewById(R.id.huowutv);
				holder.hangxianTv = (TextView) convertView
						.findViewById(R.id.hangxiantv);
				holder.jiesuanTv = (TextView) convertView
						.findViewById(R.id.jiesuanfangshitv);
				holder.yunliangTv = (TextView) convertView
						.findViewById(R.id.yunliangtv);
				holder.jiezhiTv = (TextView) convertView
						.findViewById(R.id.jiezhiriqitv);
				holder.qiwangTv = (TextView) convertView
						.findViewById(R.id.yunfeiqiwangtv);

				holder.takeOrderBt = (Button) convertView
						.findViewById(R.id.takeorderbt);

				holder.linear1 = (LinearLayout) convertView
						.findViewById(R.id.linear1);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			try {
				// 另item颜色分层
				if (position % 2 == 1) {
					holder.linear1.setBackgroundColor(getResources().getColor(
							R.color.light_gray2));
				} else {
					holder.linear1.setBackgroundColor(getResources().getColor(
							R.color.light_gray));
				}
			} catch (Exception e) {
			}
			holder.tuoyunrenTv.setText(mData.get(position)
					.get("getConsignorCompany").toString());
			holder.huowuTv.setText(mData.get(position).get("getCargoName")
					.toString());
			holder.hangxianTv.setText(mData.get(position).get("getStartPort")
					.toString()
					+ "-" + mData.get(position).get("getEndPort").toString());
			holder.jiesuanTv.setText(mData.get(position)
					.get("getAccountMethod").toString());
			holder.yunliangTv.setText(mData.get(position).get("getRAmount")
					.toString()
					+ mData.get(position).get("getCargoUnits").toString());
			holder.jiezhiTv.setText(mData.get(position).get("getLoadTime")
					.toString());
			holder.qiwangTv.setText(mData.get(position).get("getExpectPrice")
					.toString());
			holder.takeOrderBt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					FunctionOfActivity.progressDialogShow(THIS);
					try {
						Intent intent = new Intent(THIS,
								MakeToOrderActivity.class);
						intent.putExtra("getId",
								mData.get(position).get("getId").toString());
						intent.putExtra("getLoadTime",
								mData.get(position).get("getLoadTime")
										.toString());
						intent.putExtra("getLoadRequire", mData.get(position)
								.get("getLoadRequire").toString());
						intent.putExtra("getCode",
								mData.get(position).get("getCode").toString());
						intent.putExtra("getCargoName", mData.get(position)
								.get("getCargoName").toString());
						intent.putExtra("getStartPort", mData.get(position)
								.get("getStartPort").toString());
						intent.putExtra("getEndPort",
								mData.get(position).get("getEndPort")
										.toString());
						intent.putExtra("getRAmount",
								mData.get(position).get("getRAmount")
										.toString());
						intent.putExtra("getCargoUnits", mData.get(position)
								.get("getCargoUnits").toString());
						intent.putExtra("getIncludePcf", mData.get(position)
								.get("getIncludePcf").toString());
						intent.putExtra("getAccountMethod", mData.get(position)
								.get("getAccountMethod").toString());
						intent.putExtra("getPackingType", mData.get(position)
								.get("getPackingType").toString());
						intent.putExtra("getConsignorCompany",
								mData.get(position).get("getConsignorCompany")
										.toString());
						intent.putExtra("getConsignor", mData.get(position)
								.get("getConsignor").toString());
						intent.putExtra("getContact",
								mData.get(position).get("getContact")
										.toString());
						intent.putExtra("getExpectPrice", mData.get(position)
								.get("getExpectPrice").toString());
						intent.putExtra("getFareTax",
								mData.get(position).get("getFareTax")
										.toString());
						intent.putExtra("getLossrate",
								mData.get(position).get("getLossrate")
										.toString());
						intent.putExtra("getLoadUseTime", mData.get(position)
								.get("getLoadUseTime").toString());
						intent.putExtra("getDemurrage", mData.get(position)
								.get("getDemurrage").toString());
						intent.putExtra("getShipTonnages", mData.get(position)
								.get("getShipTonnages").toString());
						intent.putExtra("getRemark",
								mData.get(position).get("getRemark").toString());
						startActivity(intent);
					} catch (Exception e) {

					}
				}
			});
			return convertView;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			FunctionOfActivity.progressDialogShow(THIS);
			startActivity(new Intent(THIS, MainActivity.class));
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
